const app = getApp();
const requestUtil = require("../../../utils/request");
const util = require("../../../utils/util");
const api = require("../../../config/api");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        fyNum: 1,
        fySize: 10,
        dataList: [],
        ToolTabCur: 1,
        qIndex: 0,
        uIndex: 0,
        dIndex: 0,
        toolbars:[
            {name:"写汇报", class:"cuIcon-post", url: "/pages/question/center/center"},
            {name:"看汇报", class:"cuIcon-punch", url: "/pages/question/list/answerList"},
            {name:"统计", class:"cuIcon-crown", url: "/pages/question/total/questionTotal"},
            {name:"我的", class:"cuIcon-profile", url: "/pages/question/my/myQuestionList"},
        ],
        betDateName: null,
        dList: [
            {id: 0, name: '本月'},
            {id: 1, name:'本周'},
            {id: 2, name:'上周'},
            {id: 3, name:'上月'},
            {id: 4, name:'自定义区间'},
        ],
        uList: [],
        selectUser: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.formatData();
        this.questionList();
    },
    formatData(){
        let dList = this.data.dList;
        var date = new Date();
        var year = date.getFullYear();//本年
        var month = date.getMonth();//本月
        var week = date.getDay();//当前星期 0.1.2.3.4.5.6[0=周日]
        var time = date.getTime();//当天
        var oneDayTime = 24 * 60 * 60 * 1000;//一天的总ms

        //本月
        dList[0].startDate = util.dateFormat(date, 'yyyy-MM') + '-01';
        dList[0].endDate = util.dateFormat(date, 'yyyy-MM') + '-' + new Date(year, month+1, 0).getDate();

        //本周
        var thisWeekFirst = (1 - week) * oneDayTime + time;
        var thisWeekLast = thisWeekFirst + 6 * oneDayTime;
        dList[1].startDate = util.dateFormat(new Date(thisWeekFirst), 'yyyy-MM-dd');
        dList[1].endDate = util.dateFormat(new Date(thisWeekLast), 'yyyy-MM-dd');

        //上周
        dList[2].startDate = util.dateFormat(new Date(thisWeekFirst - 7*oneDayTime), 'yyyy-MM-dd');
        dList[2].endDate = util.dateFormat(new Date(thisWeekFirst - oneDayTime), 'yyyy-MM-dd');

        //上月
        var preDate = new Date(year, month, 0);
        var lastDay = preDate.getDate();
        dList[3].startDate = util.dateFormat(preDate, 'yyyy-MM')  + '-01';
        dList[3].endDate = util.dateFormat(preDate, 'yyyy-MM') + '-' + lastDay;

        dList[4].startDate = util.dateFormat(date,'yyyy-MM-dd');
        dList[4].endDate = util.dateFormat(date,'yyyy-MM-dd');
        this.setData({
            dList: dList,
        })
    },
    async questionList(){
        let that = this;
        let res = await requestUtil.request("POST", api.urls.questionList, {
            companyId: app.globalData.employeeInfo.companyId,
            userIds: app.globalData.employeeInfo.isAdmin == 0 ? app.globalData.employeeInfo.id : null,
        })
        let qList = [];
        for(var i in res.data.data) {
            qList.push({id: res.data.data[i].id, name: res.data.data[i].questionName, type: res.data.data[i].questionType});
        }
        that.setData({
            qList: qList,
        })
        this.initData();
        this.userList(qList[0].type == 2 ? qList[0].id : 0);
    },
    async userList(qId){
        if(qId == 0) {
            this.setData({
                uList: [],
                selectUser: '',
            })
            return;
        }
        let that = this;
        let res = await requestUtil.request("GET", api.urls.questionUserList, {questionId: qId});
        that.setData({
            uList: res.data.data,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log("上拉粗蒂")
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        
    },
    async initData(idx) {
        let that = this;
        let fyNum = that.data.fyNum;
        let dataList = that.data.dataList;
        if(idx == 0) {
            fyNum = 1;
            dataList = [];
        }
        if(fyNum < 0) return;
        let params = {
            fyNum: fyNum,
            fySize: that.data.fySize,
            companyId: app.globalData.employeeInfo.companyId,
        }
        params.startDate = that.data.dList[that.data.dIndex].startDate;
        params.endDate = that.data.dList[that.data.dIndex].endDate;
        params.questionId = that.data.qList[that.data.qIndex].id
        if(that.data.selectUser.length > 0) {
            params.userName = that.data.selectUser;
        }
        let res = await requestUtil.request("POST", api.urls.questionAnswerList, params)
        for(let i in res.data.data.rows) {
            let item = res.data.data.rows[i];
            item.answerContent = JSON.parse(item.answerContent);
            item.createTime = util.dateFormat(new Date(item.createTime),'yyyy-MM-dd HH:mm');
            if(item.questionType == 2) {
                item.status = !item.answerContent["wResult"] ? 0 : 1
            }
            dataList.push(item);
        }
        fyNum = fyNum * that.data.fySize >= res.data.data.total ? -1 : fyNum + 1;
        that.setData({
            dataList: dataList,
            fyNum: fyNum
        });
    },
    lower(){
        this.initData();
    },
    upper(){
        this.initData(0);
    },
    toolTabSelect(e){
        let index = e.currentTarget.dataset.index;
        wx:wx.navigateTo({
          url: this.data.toolbars[index].url
        })
    },
    toRedic(e) {
        let id = e.currentTarget.dataset.id;
        wx:wx.navigateTo({
          url: "/pages/question/questionDetail/questionDetail?answerId=" + id,
        })
    },
    showModal(e) {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
    },
    hideModal(e) {
        this.setData({
          modalName: null
        })
    },
    questionPickerChange(e) {
        let idx = parseInt(e.detail.value);
        if(this.data.qIndex == idx) return;
        this.setData({
          qIndex: idx
        })
        let qitem = this.data.qList[idx];
        this.initData(0);
        this.userList(qitem.type == 2 ? qitem.id : 0);
    },
    datePickerChange(e) {
        let idx = parseInt(e.detail.value);
        if(idx == 4) {
            this.setData({
                modalName: 'zdyDate'
            })
            return;
        }
        if(this.data.dIndex == idx) return;
        this.setData({
          dIndex: idx,
          betDateName: null,
        })
        this.initData(0);
    },
    changeSelectUser(e) {
        let name = e.detail.value;
        if(this.data.selectUser == name) return;
        this.setData({
            selectUser: name
        })
        this.hideModal();
        this.initData(0);
    },
    zdyDateChange(e) {
        let name = e.currentTarget.dataset.name;
        let dList = this.data.dList;
        dList[4][name] = e.detail.value;
        this.setData({
            dList: dList,
        })
    },
    sureZdyDate(e) {
        let dList = this.data.dList;
        let startStr = util.dateFormat(new Date(dList[4].startDate), 'yy/M/d');
        let endStr = util.dateFormat(new Date(dList[4].endDate),'yy/M/d');
        this.setData({
            modalName: null,
            betDateName: startStr + "-" + endStr,
            dIndex: 4
        })
        this.initData(0);
    },
})